package com.haiyou.common.config;

import com.haiyou.common.interceptor.AuthorizationInterceptor;
import com.haiyou.common.interceptor.EntranceInterceptor;
import com.haiyou.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 配置类
 */
@Configuration
@Slf4j
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new EntranceInterceptor()).addPathPatterns("/**");

        String isOpen = env.getProperty("token.open");
        log.info("------->token拦截器：{}", isOpen);
        if ("true".equals(isOpen)) {
            InterceptorRegistration interceptorRegistration = registry.addInterceptor(authorizationInterceptor);
            interceptorRegistration.addPathPatterns("/**")
                    .excludePathPatterns("/open/**", "/test/**", "/api/**", "/swagger-resources/**", "/webjars/**", "/v2/**","/error", "/swagger-ui.html");

            String diyOpenUrl = env.getProperty("token.diyOpenUrl");
            if (StringUtils.isNotEmpty(diyOpenUrl)) {
                interceptorRegistration.excludePathPatterns(diyOpenUrl.split(","));
            }
        }
        super.addInterceptors(registry);
    }
}
